<template>
    <div>
        <el-form :inline="true">
            <el-form-item label="账号">
                <el-input v-model="code" placeholder="请输入账号"></el-input>
            </el-form-item>
            <el-form-item label="">
                <el-button type="primary" @click="submitData">添加账号</el-button>
                <el-button type="primary" @click="getData" style="margin-left:100px;">查询</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="20">
            <el-table :data="codeList" border>
                <el-table-column label="账号" prop="code"></el-table-column>
                <el-table-column label="开启/关闭" prop="code">
                    <template slot-scope="scope">
                        <el-switch
                            v-model="scope.row.isClose"
                            @change="changeSwitch($event,scope.row._id)"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            active-value="1"
                            inactive-value="-1">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-checkbox-group v-model="scope.row.powerList" @change="changePower($event,scope.row._id)">
                            <el-checkbox :label="item.value" v-for="item in powerItemList" :key="item.value">{{item.label}}</el-checkbox>
                        </el-checkbox-group>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
    </div>
</template>

<script>
import { get } from '@/common/app.js'
import { powerItem } from "@/enum/zhuxian.js"
export default {
    components: {},
    data() {
        return {
            codeList: [],
            code: "",
            isEdit: false,

            powerItemList: powerItem,
        }
    },
    mounted() {

    },
    methods: {
        getData(){
            let params = {
                type: 'getCode',
                data: {
                    type: 'get',
                },
            }
            get('game', params).then(res => {
                if (res) {
                    this.codeList = res.data
                }
            })
        },
        submitData(){
            if(!this.code){
                this.$message.error("请输入账号！")
                return
            }
            let params = {
                type: 'getCode',
                data: {
                    type: 'add',
                    code: this.code,
                },
            }
            get('game', params).then(res => {
                if (res) {
                    if(res.data&&res.data.length>0){
                        this.$message.error("账号已存在")
                    }else{
                        this.$message.error("账号已存在")
                        this.getData()
                    }
                }
            })
        },
        changeSwitch(val,_id){
            let params = {
                type: 'getCode',
                data: {
                    type: 'edit',
                    isClose: val,
                    _id: _id,
                },
            }
            get('game', params).then(res => {
                if (res) {
                    this.getData()
                }
            })
        },
        changePower(val,_id){
            let params = {
                type: 'getCode',
                data: {
                    type: 'edit',
                    powerList: val,
                    _id: _id,
                },
            }
            get('game', params).then(res => {
                if (res) {
                    this.getData()
                }
            })
        }
    },
};
</script>

<style>
</style>